#include <pthread.h>
#include <iostream>
#include <string>
#include "Protocol.hpp"
#include "Sock.hpp"

void *HandlerRequest(void *args)
{
    int sock = *(int *)args;
    delete (int *)args;
    pthread_detach(pthread_self());

    // version 2 :有序列化操作
    //读取对方发送过来的数据，然后再反序列化
    char buffer[1024];
    request_t req;
    ssize_t sz = read(sock, buffer, sizeof(buffer) - 1);
    if (sz > 0) //读取成功对方发过来的序列化数据
    {
        buffer[sz] = 0;
        std::string join_string = buffer;
        DeserilizeRequest(join_string, req);

        //分析请求 && 计算结果
        response_t resp = {0, 0}; //构建响应对象
        // 1.分析请求(服务端分析发送过来的请求，该如何处理)
        switch (req.op)
        {
        case '+':
            resp.result = req.x + req.y;
            break;
        case '-':
            resp.result = req.x - req.y;

            break;
        case '*':
            resp.result = req.x * req.y;

            break;
        case '/':
            if (req.y == 0)
                resp.code = -1; //表示除0
            else
                resp.result = req.x / req.y;

            break;
        case '%':
            if (req.y == 0)
                resp.code = -2; //表示%0
            else
                resp.result = req.x % req.y;

            break;
        default:
            resp.code = -3;
            break;
        }
        std::string send_string = SerializeRespsonse(resp);   //对结构化数据 (响应)序列化发送回去
        write(sock, send_string.c_str(), send_string.size()); //响应报文发送回去
        cout << "服务结束" << endl;
    }

    close(sock);

    // version 1:无序列化操作
    //业务逻辑
    //拿到request->分析处理->构建response->send(response)->close(sock);
    // 1.读取请求对方发送过来的，存放到req变量中
    // request_t req;
    // ssize_t sz = read(sock, &req, sizeof(req));
    // if (sz == sizeof(request)) //表示请求的内容读取完毕
    // {
    //     //分析请求 && 计算结果
    //     response_t resp = {0, 0}; //构建响应对象
    //     // 1.分析请求(服务端分析发送过来的请求，该如何处理)
    //     switch (req.op)
    //     {
    //     case '+':
    //         resp.result = req.x + req.y;
    //         break;
    //     case '-':
    //         resp.result = req.x - req.y;

    //         break;
    //     case '*':
    //         resp.result = req.x * req.y;

    //         break;
    //     case '/':
    //         if (req.y == 0)
    //             resp.code = -1; //表示除0
    //         else
    //             resp.result = req.x / req.y;

    //         break;
    //     case '%':
    //         if (req.y == 0)
    //             resp.code = -2; //表示%0
    //         else
    //             resp.result = req.x % req.y;

    //         break;
    //     default:
    //         resp.code = -3;
    //         break;
    //     }
    //     write(sock, &resp, sizeof(resp)); //响应报文发送回去

    //     cout << "服务结束" << endl;
    // }
    // // 5.关闭链接
    // close(sock);
    // return (void *)0;
}
int main(int argc, char **argv)
{
    if (argc != 2)
    {
        cout << "Usage: " << argv[0] << " port" << endl;
        exit(1);
    }
    uint16_t port = atoi(argv[1]);
    int listen_sock = Sock::Socket();
    Sock::Bind(listen_sock, port);
    Sock::Listen(listen_sock);

    for (;;)
    {
        int sock = Sock::Accept(listen_sock);
        cout << "get a new link..." << endl;
        pthread_t tid;
        int *parm = new int(sock);
        pthread_create(&tid, NULL, HandlerRequest, (void *)parm);
    }
    return 0;
}
